import {
  toast
} from "../../utils/util"
import base from "../../api/base"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    options: {
      publish: {
        name: '发布',
        num: 0,
        url: '../myPubLish/index'
      },
      collect: {
        name: '收藏',
        num: 0,
        url: '../collect/index'
      },
      fans: {
        name: '粉丝',
        num: 0,
        url: '../fans/index'
      },
      balance: {
        name: '余额/元',
        num: 0,
        url: '../balance/index'
      }
    },
    skipPage: [{
        imgUrl: '../../assets/images/withdraw.png',
        url: '../../vip/withdraw/index',
        name: '余额提现'
      },
      {
        imgUrl: '../../assets/images/feedback.png',
        url: '../suggest/index',
        name: '意见反馈'
      },
      {
        imgUrl: '../../assets/images/cooperation.png',
        url: '../../vip/cooperation/index',
        name: '商务合作'
      },
      {
        imgUrl: '../../assets/images/update.png',
        url: '../../vip/upgrade/index',
        name: '会员升级'
      },
    ],
    showCooperation: false,
    headurl: '',
    nickname: '',
    level: '',
    total: 0,
    idisplaystart: 0,
    idisplaylength: 10,
    isagency: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initPersonal()
    this.getPublish()
  },
  initPersonal() {
    const {
      headurl,
      nickname,
      userlevel,
      isagency
    } = wx.getStorageSync('userInfo')
    const level = userlevel === 1 ? '普通会员' : userlevel === 2 ? '服务商' : '区域服务商'
    const person = 'person'
    this.setData({
      headurl,
      nickname,
      level,
      isagency
    })
    isagency === 1 ? this.setData({
      level: '代理商'
    }) : ''
  },
  getPublish() {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPublishNum()
    this.getCollectNum()
    this.getFansNum()
    this.getBalance()
  },
  getPublishNum() {
    const data = {
      inqurystate: 1,
      equtype: '',
      lon: 0,
      lat: 0,
      userid: wx.getStorageSync('userInfo').userid
    }
    const data2 = {
      inqurystate: 1,
      entscope: '',
      lon: 0,
      lat: 0,
      userid: wx.getStorageSync('userInfo').userid
    }
    const data3 = {
      inqurystate: 1,
      maintainscope: '',
      lon: 0,
      lat: 0,
      userid: wx.getStorageSync('userInfo').userid
    }
    const num = 'options.publish.num'
    Promise.all([app.$api.index.rentFind({
        ...data
      }), app.$api.index.saleFind({
        ...data2
      }), app.$api.index.maintainFind({
        ...data3
      })])
      .then((res) => {
        this.calculateTotal(res, num)
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  getCollectNum() {
    const {
      idisplaystart,
      idisplaylength
    } = this.data
    const infortypeRent = 1
    const infortypeSell = 2
    const infortypeMaintain = 3
    const num = 'options.collect.num'
    Promise.all([app.$api.collect.collectFind({
        infortype: infortypeRent,
        idisplaystart,
        idisplaylength
      }), app.$api.collect.collectFind({
        infortype: infortypeSell,
        idisplaystart,
        idisplaylength
      }), app.$api.collect.collectFind({
        infortype: infortypeMaintain,
        idisplaystart,
        idisplaylength
      })])
      .then((res) => {
        this.calculateTotal(res, num)
      }).catch((e) => {
        toast('请稍后再试', 1000)
      })
  },
  getFansNum() {
    const num = 'options.fans.num'
    const {
      fans
    } = wx.getStorageSync('userInfo')
    this.setData({
      [num]: fans
    })
  },
  getBalance() {
    const num = 'options.balance.num'
    const {
      balance
    } = wx.getStorageSync('userInfo')
    this.setData({
      [num]: balance
    })
  },
  calculateTotal(res, num) {
    if (!Array.isArray(res)) return
    const total = res.reduce((p, v) => {
      return p += v.data.total
    }, 0)
    this.setData({
      [num]: total
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 跳转余额提现等页面
  bindPersonPage(e) {
    const index = e.currentTarget.dataset.index
    const data = this.data.skipPage
    const url = data[index].url
    if (index === 2) {
      this.setData({
        showCooperation: true
      })
    } else {
      // if(index === 0 && !this.data.options.balance.num) {
      //   wx.showModal({
      //     title: '温馨提示',
      //     showCancel: false,
      //     content: `您的余额数为0，请尽快去添加`
      //   })
      //   return
      // }
      wx.navigateTo({
        url
      })
    }
  },
  bindMePage(e) {
    const {
      url,
      num,
      name
    } = e.currentTarget.dataset.key
    wx.navigateTo({
      url: `${url}?num=${num}`
    })
  },
  bindPersonInfo() {
    wx.navigateTo({
      url: './info/index'
    })
  },
  onClose() {
    this.setData({
      showCooperation: false
    })
  },
  // 拨打电话
  bindCall() {
    wx.makePhoneCall({
      phoneNumber: base.phone // 仅为示例，并非真实的电话号码
    })
  }
})